<?php
/**
 * Date: 2019-06-05
 * Time: 17:16
 * 切换语言只需要让url带上lang=en lang=zh lang=zh_TW，即可
 *
 */

/**
 * 翻译，不输出
 * @param $key
 * @param null $args
 * @return string
 * @throwsCantLoadDictionaryException
 * @throws CantLoadDictionaryException
 */
function _t($key, $args = null)
{
    if (!MLang) {
        if (is_array($args)) {
            return vsprintf($key, $args);
        }
        return $key;
    }
    if (!is_array($args)) {
        $args = array_slice(func_get_args(), 1);
    }

    if ($args) {
        return i18nclass::instance()->format("Generic", $key, $args);
    }
    return i18nclass::instance()->get("Generic", $key);
}

/**
 * 翻译并输出
 * @param $key
 * @param null $args
 * @throws CantLoadDictionaryException
 */
function t($key, $args = null)
{
    echo _t($key, $args);
}


/**
 * 获取支持的语言代号
 * @param bool $ass 是否仅返回单纯数组，单纯数组将不包含语言名称
 * @return array
 */
function langGetCodeArray($ass = false)
{
    $codeSets = array(
        'zh_MO.UTF-8', 'zh_HK.UTF-8', 'zh_SG.UTF-8',
        'af_ZA.UTF-8', 'sq_AL.UTF-8', 'ar_SA.UTF-8', 'eu_ES.UTF-8', 'be_BY.UTF-8', 'bs_BA.UTF-8', 'bg_BG.UTF-8',
        'ca_ES.UTF-8', 'hr_HR.UTF-8', 'zh_CN.UTF-8', 'zh_TW.UTF-8', 'cs_CZ.UTF-8', 'da_DK.UTF-8', 'nl_NL.UTF-8',
        'en.UTF-8', 'et_EE.UTF-8', 'fa_IR.UTF-8', 'ph_PH.UTF-8', 'fi_FI.UTF-8', 'fr_FR.UTF-8', 'fr_CH.UTF-8',
        'fr_BE.UTF-8', 'fr_CA.UTF-8', 'ga.UTF-8', 'gl_ES.UTF-8', 'ka_GE.UTF-8', 'de_DE.UTF-8', 'de_DE.UTF-8',
        'el_GR.UTF-8', 'gu.UTF-8', 'he_IL.utf8', 'hi_IN.UTF-8', 'hu.UTF-8', 'is_IS.UTF-8', 'id_ID.UTF-8',
        'it_IT.UTF-8', 'ja_JP.UTF-8', 'kn_IN.UTF-8', 'km_KH.UTF-8', 'ko_KR.UTF-8', 'lo_LA.UTF-8', 'lt_LT.UTF-8',
        'lat.UTF-8', 'ml_IN.UTF-8', 'ms_MY.UTF-8', 'mi_NZ.UTF-8', 'mi_NZ.UTF-8', 'mn.UTF-8', 'no_NO.UTF-8',
        'no_NO.UTF-8', 'nn_NO.UTF-8', 'pl.UTF-8', 'pt_PT.UTF-8', 'pt_BR.UTF-8', 'ro_RO.UTF-8', 'ru_RU.UTF-8',
        'mi_NZ.UTF-8', 'sr_CS.UTF-8', 'sk_SK.UTF-8', 'sl_SI.UTF-8', 'so_SO.UTF-8', 'es_ES.UTF-8', 'sv_SE.UTF-8',
        'tl.UTF-8', 'ta_IN.UTF-8', 'th_TH.UTF-8', 'mi_NZ.UTF-8', 'tr_TR.UTF-8', 'uk_UA.UTF-8', 'vi_VN.UTF-8'
    );

    $codeCoun = array(
        "af" => "公用荷兰语",
        "af-ZA" => "公用荷兰语-南非",
        "sq" => "阿尔巴尼亚",
        "sq-AL" => "阿尔巴尼亚-阿尔巴尼亚",
        "ar" => "阿拉伯语",
        "ar-DZ" => "阿拉伯语-阿尔及利亚",
        "ar-BH" => "阿拉伯语-巴林",
        "ar-EG" => "阿拉伯语-埃及",
        "ar-IQ" => "阿拉伯语-伊拉克",
        "ar-JO" => "阿拉伯语-约旦",
        "ar-KW" => "阿拉伯语-科威特",
        "ar-LB" => "阿拉伯语-黎巴嫩",
        "ar-LY" => "阿拉伯语-利比亚",
        "ar-MA" => "阿拉伯语-摩洛哥",
        "ar-OM" => "阿拉伯语-阿曼",
        "ar-QA" => "阿拉伯语-卡塔尔",
        "ar-SA" => "阿拉伯语-沙特阿拉伯",
        "ar-SY" => "阿拉伯语-叙利亚共和国",
        "ar-TN" => "阿拉伯语-北非的共和国",
        "ar-AE" => "阿拉伯语-阿拉伯联合酋长国",
        "ar-YE" => "阿拉伯语-也门",
        "hy" => "亚美尼亚",
        "hy-AM" => "亚美尼亚的-亚美尼亚",
        "az" => "Azeri",
        "az-AZ-Cyrl" => "Azeri-(西里尔字母的)阿塞拜疆",
        "az-AZ-Latn" => "Azeri(拉丁文)-阿塞拜疆",
        "eu" => "巴斯克",
        "eu-ES" => "巴斯克-巴斯克",
        "be" => "Belarusian",
        "be-BY" => "Belarusian-白俄罗斯",
        "bg" => "保加利亚",
        "bg-BG" => "保加利亚-保加利亚",
        "ca" => "嘉泰罗尼亚",
        "ca-ES" => "嘉泰罗尼亚-嘉泰罗尼亚",
        "zh" => "中文",
        "zh-HK" => "中文-香港的SAR",
        "zh-MO" => "中文-澳门的SAR",
        "zh-CN" => "中文-中国",
        "zh-CHS" => "中文(单一化)",
        "zh-SG" => "中文-新加坡",
        "zh-TW" => "中文-台湾",
        "zh-CHT" => "中文(传统的)",
        "hr" => "克罗埃西亚",
        "hr-HR" => "克罗埃西亚-克罗埃西亚",
        "cs" => "捷克",
        "cs-CZ" => "捷克-捷克",
        "da" => "丹麦文",
        "da-DK" => "丹麦文-丹麦",
        "div" => "Dhivehi",
        "div-MV" => "Dhivehi-马尔代夫",
        "nl" => "荷兰",
        "nl-BE" => "荷兰-比利时",
        "nl-NL" => "荷兰-荷兰",
        "en" => "英国",
        "en-AU" => "英国-澳洲",
        "en-BZ" => "英国-伯利兹",
        "en-CA" => "英国-加拿大",
        "en-CB" => "英国-加勒比海",
        "en-IE" => "英国-爱尔兰",
        "en-JM" => "英国-牙买加",
        "en-NZ" => "英国-新西兰",
        "en-PH" => "英国-菲律宾共和国",
        "en-ZA" => "英国-南非",
        "en-TT" => "英国-千里达托贝哥共和国",
        "en-GB" => "英国-英国",
        "en-US" => "英国-美国",
        "en-ZW" => "英国-津巴布韦",
        "et" => "爱沙尼亚",
        "et-EE" => "爱沙尼亚的-爱沙尼亚",
        "fo" => "Faroese",
        "fo-FO" => "Faroese-法罗群岛",
        "fa" => "波斯语",
        "fa-IR" => "波斯语-伊朗王国",
        "fi" => "芬兰语",
        "fi-FI" => "芬兰语-芬兰",
        "fr" => "法国",
        "fr-BE" => "法国-比利时",
        "fr-CA" => "法国-加拿大",
        "fr-FR" => "法国-法国",
        "fr-LU" => "法国-卢森堡",
        "fr-MC" => "法国-摩纳哥",
        "fr-CH" => "法国-瑞士",
        "gl" => "加利西亚",
        "gl-ES" => "加利西亚-加利西亚",
        "ka" => "格鲁吉亚州",
        "ka-GE" => "格鲁吉亚州-格鲁吉亚州",
        "de" => "德国",
        "de-AT" => "德国-奥地利",
        "de-DE" => "德国-德国",
        "de-LI" => "德国-列支敦士登",
        "de-LU" => "德国-卢森堡",
        "de-CH" => "德国-瑞士",
        "el" => "希腊",
        "el-GR" => "希腊-希腊",
        "gu" => "Gujarati",
        "gu-IN" => "Gujarati-印度",
        "he" => "希伯来",
        "he-IL" => "希伯来-以色列",
        "hi" => "北印度语",
        "hi-IN" => "北印度的-印度",
        "hu" => "匈牙利",
        "hu-HU" => "匈牙利的-匈牙利",
        "is" => "冰岛语",
        "is-IS" => "冰岛的-冰岛",
        "id" => "印尼",
        "id-ID" => "印尼-印尼",
        "it" => "意大利",
        "it-IT" => "意大利-意大利",
        "it-CH" => "意大利-瑞士",
        "ja" => "日本",
        "ja-JP" => "日本-日本",
        "kn" => "卡纳达语",
        "kn-IN" => "卡纳达语-印度",
        "kk" => "Kazakh",
        "kk-KZ" => "Kazakh-哈萨克",
        "kok" => "Konkani",
        "kok-IN" => "Konkani-印度",
        "ko" => "韩国",
        "ko-KR" => "韩国-韩国",
        "ky" => "Kyrgyz",
        "ky-KZ" => "Kyrgyz-哈萨克",
        "lv" => "拉脱维亚",
        "lv-LV" => "拉脱维亚的-拉脱维亚",
        "lt" => "立陶宛",
        "lt-LT" => "立陶宛-立陶宛",
        "mk" => "马其顿",
        "mk-MK" => "马其顿-FYROM",
        "ms" => "马来",
        "ms-BN" => "马来-汶莱",
        "ms-MY" => "马来-马来西亚",
        "mr" => "马拉地语",
        "mr-IN" => "马拉地语-印度",
        "mn" => "蒙古",
        "mn-MN" => "蒙古-蒙古",
        "no" => "挪威",
        "nb-NO" => "挪威(Bokmål)-挪威",
        "nn-NO" => "挪威(Nynorsk)-挪威",
        "pl" => "波兰",
        "pl-PL" => "波兰-波兰",
        "pt" => "葡萄牙",
        "pt-BR" => "葡萄牙-巴西",
        "pt-PT" => "葡萄牙-葡萄牙",
        "pa" => "Punjab语",
        "pa-IN" => "Punjab语-印度",
        "ro" => "罗马尼亚语",
        "ro-RO" => "罗马尼亚语-罗马尼亚",
        "ru" => "俄国",
        "ru-RU" => "俄国-俄国",
        "sa" => "梵文",
        "sa-IN" => "梵文-印度",
        "sr-SP-Cyrl" => "塞尔维亚-(西里尔字母的)塞尔维亚共和国",
        "sr-SP-Latn" => "塞尔维亚(拉丁文)-塞尔维亚共和国",
        "sk" => "斯洛伐克",
        "sk-SK" => "斯洛伐克-斯洛伐克",
        "sl" => "斯洛文尼亚",
        "sl-SI" => "斯洛文尼亚-斯洛文尼亚",
        "es" => "西班牙",
        "es-AR" => "西班牙-阿根廷",
        "es-BO" => "西班牙-玻利维亚",
        "es-CL" => "西班牙-智利",
        "es-CO" => "西班牙-哥伦比亚",
        "es-CR" => "西班牙-哥斯达黎加",
        "es-DO" => "西班牙-多米尼加共和国",
        "es-EC" => "西班牙-厄瓜多尔",
        "es-SV" => "西班牙-萨尔瓦多",
        "es-GT" => "西班牙-危地马拉",
        "es-HN" => "西班牙-洪都拉斯",
        "es-MX" => "西班牙-墨西哥",
        "es-NI" => "西班牙-尼加拉瓜",
        "es-PA" => "西班牙-巴拿马",
        "es-PY" => "西班牙-巴拉圭",
        "es-PE" => "西班牙-秘鲁",
        "es-PR" => "西班牙-波多黎各",
        "es-ES" => "西班牙-西班牙",
        "es-UY" => "西班牙-乌拉圭",
        "es-VE" => "西班牙-委内瑞拉",
        "sw" => "Swahili",
        "sw-KE" => "Swahili-肯尼亚",
        "sv" => "瑞典",
        "sv-FI" => "瑞典-芬兰",
        "sv-SE" => "瑞典-瑞典",
        "syr" => "Syriac",
        "syr-SY" => "Syriac-叙利亚共和国",
        "ta" => "坦米尔",
        "ta-IN" => "坦米尔-印度",
        "tt" => "Tatar",
        "tt-RU" => "Tatar-俄国",
        "te" => "Telugu",
        "te-IN" => "Telugu-印度",
        "th" => "泰国",
        "th-TH" => "泰国-泰国",
        "tr" => "土耳其语",
        "tr-TR" => "土耳其语-土耳其",
        "uk" => "乌克兰",
        "uk-UA" => "乌克兰-乌克兰",
        "ur" => "Urdu",
        "ur-PK" => "Urdu-巴基斯坦",
        "uz" => "Uzbek",
        "uz-UZ-Cyrl" => "Uzbek(西里尔字母的)乌兹别克斯坦",
        "uz-UZ-Latn" => "Uzbek(拉丁文)乌兹别克斯坦",
        "vi" => "越南",
        "vi-VN" => "越南-越南",);
    $codeSetsShort = array();
    $codeSetsLong = array();
    foreach ($codeSets as $v):
        $v2 = str_replace(".UTF-8", "", $v);
        $v2 = str_replace("_", "-", $v2);
        $codeSetsLong[] = $v2;
        $v2 = explode("-", $v2)[0];
        $codeSetsShort[] = $v2;
    endforeach;
    $v3 = array_merge($codeSetsShort, $codeSetsLong);
    $v3 = array_unique($v3);
    sort($v3);
    $v4 = array();
    foreach ($v3 as $v):
        if ($ass) {
            $v = str_replace("-", "_", $v);
            $v4[] = $v;
        } else {
            $vs = isset($codeCoun[$v]) ? $codeCoun[$v] : $v;
            $v = str_replace("-", "_", $v);
            $v4[$v] = $vs;
        }
    endforeach;

    return $v4;
}

/**
 * 获取已存在的语言文件.json
 * @return array
 */
function langGetLangFiles()
{
    $langs_dir = AppDir . "languages/";
    $langs = file_getlist($langs_dir);
    $key_id = array_search('setting.json', $langs);
    if ($key_id) {
        unset($langs[$key_id]);
    }

    $setting = @file_get_contents($langs_dir . 'setting.json');
    if ($setting) {
        $setting = json_decode($setting, true);
    } else {
        $setting = array();
    }

    $ret = array();

    foreach ($langs as $k => $v) {
        if (substr($v, -4) != 'json') continue;
        $settingkey = $v;
        $havesetting = false;
        for ($i = 0; $i < count($setting); $i++) {
            $set_v = $setting[$i];
            if ($set_v['file'] == $settingkey) {
                $settingVal = $set_v;
                $havesetting = true;
                break;
            }
        }
        if (!$havesetting) {
            $settingVal = array("file" => $v, "icon" => "", "name" => explode('.', $v)[0], "sort" => $k * 100);
        }
        $ret[] = $settingVal;
    }

    if (!file_exists($langs_dir . 'setting.json')) {
        file_put_contents($langs_dir . 'setting.json', json_encode($ret));
    }
    return $ret;
}

/**
 * 当前已经加载的语言文件
 * @return string
 */
function langGetLoadedLangFile()
{
    global $i18n;
    return $i18n->getLoadedLang();
}

/**
 * 获取语言文件的内容
 * @param $langFileName
 * @return array|mixed
 */
function langGetFile($langFileName)
{
    $langfile = AppDir . "languages/{$langFileName}";
    if (file_exists($langfile)) {
        $data = file_get_contents($langfile);
        if ($data) {
            $data = json_decode($data, true);
            if (isset($data['Generic'])) {
                return $data['Generic'];
            }
        }
    }
    return array();
}

/**
 * 写入语言文件
 * @param $langFileName
 * @param $data
 * @return false|int
 */
function langPutFile($langFileName, $data)
{
    $langfile = AppDir . "languages/{$langFileName}";
    $savedata['Generic'] = $data;
    $encodeData = json_encode($savedata);
    return file_put_contents($langfile, $encodeData);
}

/**
 * 设置所有键
 * @param array-key $keys 关联数组
 * @return false|int
 */
function langSetKeys($keys)
{
    $langfile = AppDir . "languages/keys.txt";
    $encodeData = json_encode($keys);
    return file_put_contents($langfile, $encodeData);
}

/**
 * 获取所有键
 * @return array|mixed
 */
function langGetKeys()
{
    $langs_dir = AppDir . "languages/";
    $ret = array();
    if (!file_exists($langs_dir . 'keys.txt')) {
        file_put_contents($langs_dir . 'keys.txt', json_encode($ret));
    } else {
        $key_str = file_get_contents($langs_dir . 'keys.txt');
        $ret = json_decode($key_str, true);
    }

    return $ret;
}

